<?php 
header('Content-Type: text/html; charset=utf-8');
error_reporting();
include '../common/config.php';
include '../common/db.php';
include '../common/function.php';
$db = new db();
//获取微信回调的数据
$xml = $GLOBALS['HTTP_RAW_POST_DATA']; 
//将xml格式转为数组
$data = xmlToArray($xml);
$order_sn = $data['out_trade_no'];
$total_fee = $data['total_fee'].'.00';
if($data['return_code'] == 'FAIL'){
	//修改为无效订单
	$sql = "update web_memberOrders set status=2 where order_sn='$order_sn'";
	$db -> add($sql);
	echo 'error';	exit();
}else{
	//查询订单是否处理过
	$sql ="select * from web_memberOrders where order_sn='$order_sn'";
	$orders = $db -> getRow($sql);
	if(!$orders){
		echo 'error';	exit();
	}
	if($orders['status'] != 0){
		echo 'error';	exit();
	}
	//判断金额是否正确
	if($total_fee != $orders['amount']){
		//冻结该账户,写入日志
		$sql = "update web_member set disable=1 where agentid=".$orders['agentID'];
		$db -> add($sql);
		$sql = "insert into web_logs(desc,status,create_time)values('订单号为".$order_sn."的订单支付回调金额异常,系统自动将该订单用户账号为".$orders['agentID']."冻结',1,".time().")";
		$db -> add($sql);
		echo 'error';
		exit();
	}
	//修改订单状态，并将用户升级为代理
	$sql = "update web_member set group_id=1 where agentid=".$orders['agentID'];
	$db ->add($sql);
	//用户充值数
	$sql = "update web_member set monetary = monetary+".$orders['amount']." where agentid=".$orders['agentID'];
	$db -> add($sql);
	$sql = "update web_memberOrders set status=1 where order_sn='$order_sn'";
	$res = $db -> add($sql);
	if($res){
		echo 'success';
		exit();
	}else{
		echo 'error';
		exit();
	}
}
?>

